<?php defined('SYSPATH') or die('No direct script access.');

class Book_Tucao{

// Book_Tucao instances
protected static $_instance;

public static function instance()
{
	if ( ! isset(Book_Tucao::$_instance))
	{
		// Create a new session instance
		Book_Tucao::$_instance = new Book_Tucao();
	}

	return Book_Tucao::$_instance;
}

public function render_thumb($image_file_name)
{
	$thumb_image_path = Kohana::$config->load('upload')->get('thumb_tucao').$image_file_name;
	if (!file_exists($thumb_image_path))
	{
		$picture_image_path = Kohana::$config->load('upload')->get('tucao').$image_file_name;
		if (!file_exists($picture_image_path))
		{
			header("Status: 400 缩略图不存在&picture图片不存在");
			exit;
		}
		else
		{
			$this->scare_image($picture_image_path, $thumb_image_path);
		}
	}
	Image_Output::write($thumb_image_path);
}

public function render($image_file_name)
{
	$image_path = Kohana::$config->load('upload')->get('tucao').$image_file_name;
	if (!file_exists($image_path))
	{
		header("Status: 400 图不存在&tucao图片不存在");
		exit;
	}
	Image_Output::write($image_path);
}

public function scare_image($picture_image_path, $thumb_image_path)
{
	try
	{ 
		$image = new Kohana_Image_GD($picture_image_path);
		$max_width = Kohana::$config->load('upload')->get('max_thumb_width');
		$max_height = Kohana::$config->load('upload')->get('max_thumb_height');
		if ($image->width > $max_width OR  $image->height > $max_height)
		{
			$image->resize($max_width, $max_height, Image::AUTO);
		}
		$image->save($thumb_image_path, 75);
	} 
	catch (Kohana_Exception $e)
	{
		header("Status: 400 picture图片错误");
		exit;
	}
}

public function create_tucao_list($array)
{
	$object = array();
	foreach ( $array as $tucao)
	{
		array_push($object,
			array(
				'id'			=> $tucao->id,
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}

}




